package 力扣题库;

import java.util.ArrayList;
import java.util.List;

public class p257二叉树的所有路径 {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res=new ArrayList<>();
        if (root==null) {
            return res;
        }
        constructPath(root,"",res);
        return res;

    }
    public void constructPath(TreeNode root,String path,List<String> res) {
        StringBuilder finallyPath=new StringBuilder(path);
        if (root!=null) {
            finallyPath.append(root.val);
            if (root.right==null && root.left==null) {
                //到叶子结点了
                res.add(finallyPath.toString());
            }else {
                finallyPath.append("->");
                constructPath(root.left,finallyPath.toString(),res);
                constructPath(root.right,finallyPath.toString(),res);
            }
        }
    }
}
